package com.jic.market.mapper;

import com.jic.market.entity.PromotionMemberGrade;
import com.jic.market.orm.BaseMapper;
import com.jic.market.entity.PromotionMerchantEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PromotionMerchantEntityMapper extends BaseMapper<PromotionMerchantEntity>{

    Integer updateByPromotionId(PromotionMerchantEntity entity);

    void deleteByPromotionId(@Param("promotionId") Long promotionId);

    List<Long> findAllByMerchantIds(@Param("merchantIds") List<Long> merchantIds);

    List<Long> findAllByStoreIds( @Param("storeIds") List<Long> storeIds);

    void deletStoreByMerchantIdAndPromotionId(@Param("merchantId")Long merchantId, @Param("promotionId")Long promotionId);

    void deleteMerchantByPromotionId(@Param("merchantId")Long merchantId, @Param("promotionId")Long promotionId);

    void deleteStoreByPromotionId(@Param("storeId")Long storeId, @Param("promotionId")Long promotionId);

    @Select("select store_id from promotion_merchant where delete_flag = '0' and promotion_id = #{promotionId} and merchant_type = '2' ")
    List<Long> getStoreIdsByPromotionId(String promotionId);

    @Select({
            "<script>",
            "select count(1) from promotion_merchant where promotion_id=#{promotionId} and delete_flag='0'",
            "<if test=\"merchantIds !=null and merchantIds.size >0\">",
            " and merchant_id in ",
            "<foreach collection=\"merchantIds\" item=\"merchantId\" index=\"index\"",
            "                         open=\"(\" close=\")\" separator=\",\">",
            "                    #{merchantId}",
            "                </foreach>",
            "</if> ",
            "<if test=\"storeIds !=null and storeIds.size >0\">",
            "   and store_id in ",
            "   <foreach collection=\"storeIds\" item=\"storeId\" index=\"index\"",
            "                         open=\"(\" close=\")\" separator=\",\">",
            "                    #{storeId}",
            "                </foreach>",
            "</if> ",
            "<if test=\"merchantType !=null\">",
            "  and merchant_type = #{merchantType}" ,
            "</if>",
            "</script>"
    })
    Long queryByPromotionIdTotal(@Param("promotionId") Long promotionId,@Param("merchantIds") List<Long> merchantIds,@Param("storeIds") List<Long> storeIds,@Param("merchantType") Integer code);

    int insertBatch(@Param("items") List<PromotionMerchantEntity> items);
}